#世界杯点球游戏
import random;
from Sort import testSort

def kick():
    for i in range(2):
        # 玩家通过input获取输入
        you = int(input(f'{kicker[i]}罚球，请玩家选择方向（1）左 （2）中 （3）右'));
        # 电脑随机选择，使用的random随机数模块进行产生
        com = random.randint(1, 3);
        print('电脑选择:', com);
        if you != com:
            print('球进了！');
            score[i] +=1
        else:
            print('球没进！');
        #输出比分
        print(f'比分:{score[0]} : {score[1]}');

        #获取玩家对手的index，用于判断是否剩余次数是否满足直接胜利条件
        if chance[i] > 0:
            # 每次踢球之后，机会-1
            chance[i] -=1;
            j = 1-i;
            #进行判断一方的分数是否超过另一方的机会数和得分数
            if(score[i]>score[j]+chance[j]) or (score[j] > score[i]+chance[i]):
                return True;
    return False;

list_score = []

for k in range(5):
    print("第" + str(k + 1) + "位玩家")
    kicker = ['玩家', '电脑'];
    # score比分
    score = [0, 0];
    # chance记录二方剩余机会数
    chance = [5, 5];

    # 罚球轮次，共5次罚球机会，定义n从0开始，
    n = 0;
    # 判断五轮踢完之后，如果是平局情况下，加上循环，如果双方分数相同，就一直踢下去；
    while n < 5 or score[0] == score[1]:
        n += 1;
        print(f'----------第{n}轮-----------');
        # 判断函数返回值，如果是True就直接结束，
        if kick():
            break;

    if score[0] > score[1]:
        print('玩家胜利！');
        player_score = score[0] - score[1] + chance[1]
        list_score.append(player_score)
    else:
        print('电脑胜利！');
        player_score = score[0] - score[1] - chance[1]
        list_score.append(player_score)
    print(player_score)

player_name = ["玩家1","玩家2","玩家3","玩家4","玩家5"]
print(list_score)
testsort = testSort()
testsort.quick_sort(list_score, 0, len(list_score) - 1, player_name)
print(player_name)

